package com.battery.service.impl;

import com.battery.dao.FeedbackDAO;
import com.battery.pojo.Feedback;
import com.battery.service.FeedbackService;
import com.battery.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class FeedbackImpl implements FeedbackService {

    @Autowired(required = false)
    private FeedbackDAO feedbackdao;

    @Override
    public Result updateFeedbackStatus(Feedback feedback){
        try{
            if (feedbackdao.updateFeedback(feedback)>0){
                return Result.ok().message("更改反馈状态成功").code(1);
            }
            else {
                return Result.error().message("更改反馈状态失败，请重试");
            }
        }catch (Exception e){
            return Result.error().message("服务器出错,更改反馈状态失败");
        }
    }

    @Override
    public Result deleteFeedback(int id){
        try {
            if (feedbackdao.deleteFeedback(id)>0){
                return Result.ok().message("删除反馈成功");
            }
            else {
                return Result.error().message("删除反馈失败，请重试");
            }
        } catch (Exception e){
            return Result.error().message("服务器出错");
        }
    }

    @Override
    public Result getAllFeedback(){
        List <Feedback> result = new ArrayList<>();
        try {
            result = feedbackdao.getAllFeedback();
            if (result!=null){
                return Result.ok().data("allFeedback",result);
            }
            else {
                return Result.error().message("获取数据出错");
            }
        } catch (Exception e){
            return Result.error().message("出错");
        }
    }

    @Override
    public Result selectFeedback(String keyword){
        List <Feedback> result = new ArrayList<>();
        try {
            result = feedbackdao.selectFeedback(keyword);
            if (result != null){
                return Result.ok().data("selectedFeedback",result);
            }
            else {
                return Result.error().message("出错");
            }
        } catch (Exception e){
            return Result.error().message("出错");
        }
    }



}
